package com.etone.smartAudit.vo.dataModel;

import com.etone.smartAudit.enums.DataBaseType;
import com.etone.smartAudit.vo.item.ItemSimpleListVO;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: sovy
 * @date: 2020/11/10
 * @description:
 */
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
@ApiModel(value = "数据模型详情列表")
public class DataModelListVO {

    @ApiModelProperty("模型id")
    private Integer id;

    @ApiModelProperty("中文表名")
    private String cnTableName;

    @ApiModelProperty("英文表名")
    private String enTableName;

    @ApiModelProperty("一级分类")
    private String level1;

    @ApiModelProperty("二级分类")
    private String level2;

    @ApiModelProperty("三级分类")
    private String level3;

    @ApiModelProperty("服务器ip")
    private String serverIp;

    @ApiModelProperty("服务器id")
    private Integer serverId;

    @ApiModelProperty("数据库类型")
    private DataBaseType dataBaseType;

    @ApiModelProperty("数据库库名")
    private String databaseName;

    @ApiModelProperty("schema")
    private String schemaName;

    @ApiModelProperty("项目id")
    private String itemId;

    @ApiModelProperty("项目名称")
    private String itemName;

    @ApiModelProperty("是否授权")
    private Boolean isAuth;

    @ApiModelProperty("已授权项目")
    private List<ItemSimpleListVO> items;

    @ApiModelProperty("已授权项目")
    @JsonIgnore
    private String itemIdsStr;

    /**
     * 设置授权项目和是否授权
     *
     * @param itemIdsStr
     */
    public void setItemIdsStr(String itemIdsStr) {
        this.itemIdsStr = itemIdsStr;
        if (!StringUtils.isEmpty(this.itemIdsStr)) {
            this.isAuth = Boolean.TRUE;
            items = Arrays.asList(this.itemIdsStr.split(",")).stream().map(itemIdStr -> ItemSimpleListVO.builder().id(Integer.parseInt(itemIdStr)).build()).collect(Collectors.toList());
        } else {
            this.isAuth = Boolean.FALSE;
        }
    }

}
